package com.mall.controller;

import com.mall.pojo.Order;
import com.mall.pojo.dto.OrderCreateDTO;
import com.mall.pojo.vo.CheckoutVO;
import com.mall.pojo.vo.OrderListVO;
import com.mall.service.OrderService;
import com.mall.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 订单控制器
 */
@RestController
@RequestMapping("/member/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 获取结算页面数据
     */
    @GetMapping("/pre")
    public Result<CheckoutVO> getCheckoutInfo() {
        CheckoutVO checkoutVO = orderService.getCheckoutInfo();
        return Result.success(checkoutVO);
    }

    /**
     * 创建订单
     */
    @PostMapping
    public Result<Order> createOrder(@RequestBody OrderCreateDTO orderCreateDTO) {
        Order order = orderService.createOrder(orderCreateDTO);
        return Result.success(order);
    }

    /**
     * 获取订单详情
     */
    @GetMapping("/{id}")
    public Result<Order> getOrderDetail(@PathVariable Long id) {
        Order order = orderService.getOrderById(id);
        return Result.success(order);
    }
    
    /**
     * 获取我的订单列表
     */
    @GetMapping("/list")
    public Result<OrderListVO> getOrderList(
            @RequestParam(defaultValue = "0") Integer orderState,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        OrderListVO orderListVO = orderService.getOrderList(orderState, page, pageSize);
        return Result.success(orderListVO);
    }
    
    /**
     * 取消订单
     */
    @PutMapping("/{id}/cancel")
    public Result<Boolean> cancelOrder(@PathVariable Long id, @RequestBody Map<String, String> params) {
        String cancelReason = params.getOrDefault("cancelReason", "用户取消");
        boolean result = orderService.cancelOrder(id, cancelReason);
        return Result.success(result);
    }
    
    /**
     * 删除订单
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteOrder(@PathVariable Long id) {
        boolean result = orderService.deleteOrder(id);
        return Result.success(result);
    }
    
    /**
     * 确认收货
     */
    @PutMapping("/{id}/receipt")
    public Result<Boolean> confirmReceiptOrder(@PathVariable Long id) {
        boolean result = orderService.confirmReceipt(id);
        return Result.success(result);
    }
} 